package com.yj.ccs.abs.web.asset.controller;

import com.yj.ccs.abs.boot.authorization.annotation.CurrentUser;
import com.yj.ccs.abs.boot.authorization.annotation.UserRole;
import com.yj.ccs.abs.common.dto.IdDto;
import com.yj.ccs.abs.common.enums.AssetStatusType;
import com.yj.ccs.abs.common.enums.FundRestreamSourceType;
import com.yj.ccs.abs.common.enums.UserRoleType;
import com.yj.ccs.abs.common.model.BaseResult;
import com.yj.ccs.abs.web.asset.dto.*;
import com.yj.ccs.abs.web.asset.entity.*;
import com.yj.ccs.abs.web.asset.service.AssetDetailService;
import com.yj.ccs.abs.web.management.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/22
 * Author     : Unbelievable9
 **/
@Api(tags = "Web-Asset-Detail-Controller")
@RestController
@RequestMapping("/web/asset/detail")
public class AssetDetailController {

    final private AssetDetailService assetDetailService;

    @Autowired
    public AssetDetailController(AssetDetailService assetDetailService) {
        this.assetDetailService = assetDetailService;
    }

    @ApiOperation("基础信息更新")
    @PostMapping("/update-basic-info")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> updateBasicInfo(@Validated @RequestBody UpdateBasicInfoDto updateBasicInfoDto, @CurrentUser User user) {
        Asset asset = assetDetailService.getAssetById(updateBasicInfoDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            assetDetailService.updateBasicInfo(updateBasicInfoDto);

            return new ResponseEntity<>(BaseResult.success("基础信息更新成功"), HttpStatus.OK);
        }
    }

    @ApiOperation("合同信息更新")
    @PostMapping("/update-contract")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> updateContract(@Validated @RequestBody UpdateContractDto updateContractDto, @CurrentUser User user) {
        Asset asset = assetDetailService.getAssetById(updateContractDto.getAsset_id());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            if (asset.getStatus().getValue() < AssetStatusType.IN_POOL.getValue()) {
                Contract contract = assetDetailService.getContractById(updateContractDto.getId());

                if (contract == null) {
                    return new ResponseEntity<>(BaseResult.error("合同信息不存在"), HttpStatus.OK);
                } else {
                    assetDetailService.updateContract(updateContractDto);

                    return new ResponseEntity<>(BaseResult.success("合同信息更新成功"), HttpStatus.OK);
                }
            } else {
                return new ResponseEntity<>(BaseResult.error("资产已入池, 无法更新合同信息"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("货权信息更新")
    @PostMapping("/update-property-ownership")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> updatePropertyOwnership(@Validated @RequestBody UpdatePropertyOwnershipDto updatePropertyOwnershipDto, @CurrentUser User user) {
        Asset asset = assetDetailService.getAssetById(updatePropertyOwnershipDto.getAsset_id());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            if (asset.getStatus().getValue() < AssetStatusType.IN_POOL.getValue()) {
                PropertyOwnership propertyOwnership = assetDetailService.getPropertyOwnershipById(updatePropertyOwnershipDto.getId());

                if (propertyOwnership == null) {
                    return new ResponseEntity<>(BaseResult.error("货权信息不存在"), HttpStatus.OK);
                } else {
                    assetDetailService.updatePropertyOwnership(updatePropertyOwnershipDto);

                    return new ResponseEntity<>(BaseResult.success("货权信息更新成功"), HttpStatus.OK);
                }
            } else {
                return new ResponseEntity<>(BaseResult.error("资产已入池, 无法更新货权信息"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("添加发票")
    @PostMapping("/add-receipt")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> addReceipt(@Validated @RequestBody CreateReceiptDto createReceiptDto, @CurrentUser User user) {
        Asset asset = assetDetailService.getAssetById(createReceiptDto.getAsset_id());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法添加发票"), HttpStatus.OK);
            } else {
                assetDetailService.createReceipt(createReceiptDto);

                return new ResponseEntity<>(BaseResult.success("添加发票成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("发票信息更新")
    @PostMapping("/update-receipt")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> updateReceipt(@Validated @RequestBody UpdateReceiptDto updateReceiptDto, @CurrentUser User user) {
        Receipt receipt = assetDetailService.getReceiptById(updateReceiptDto.getId());

        if (receipt == null) {
            return new ResponseEntity<>(BaseResult.error("发票信息不存在"), HttpStatus.OK);
        } else {
            Asset asset = assetDetailService.getAssetById(receipt.getAsset_id());

            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法更新发票信息"), HttpStatus.OK);
            } else {
                assetDetailService.updateReceipt(updateReceiptDto);

                return new ResponseEntity<>(BaseResult.success("发票信息更新成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("删除发票信息")
    @PostMapping("/delete-receipt")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> deleteReceipt(@Validated @RequestBody IdDto idDto, @CurrentUser User user) {
        Receipt receipt = assetDetailService.getReceiptById(idDto.getId());

        if (receipt == null) {
            return new ResponseEntity<>(BaseResult.error("发票信息不存在"), HttpStatus.OK);
        } else {
            Asset asset = assetDetailService.getAssetById(receipt.getAsset_id());

            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法删除发票信息"), HttpStatus.OK);
            } else {
                assetDetailService.deleteReceipt(receipt.getId());

                return new ResponseEntity<>(BaseResult.success("发票信息删除成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("获取发票详情")
    @GetMapping("/receipt-detail")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> receiptDetail(@Validated IdDto idDto, @CurrentUser User user) {
        Receipt receipt = assetDetailService.getReceiptById(idDto.getId());

        if (receipt == null) {
            return new ResponseEntity<>(BaseResult.error("发票信息不存在"), HttpStatus.OK);
        } else {
            ReceiptDetail receiptDetail = assetDetailService.getReceiptDetail(receipt);

            return new ResponseEntity<>(BaseResult.success("获取发票详情成功").setData(receiptDetail), HttpStatus.OK);
        }
    }

    @ApiOperation("添加结算")
    @PostMapping("/add-settle-account")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> addSettleAccount(@Validated @RequestBody CreateSettleAccountDto createSettleAccountDto, @CurrentUser User user) {
        Asset asset = assetDetailService.getAssetById(createSettleAccountDto.getAsset_id());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法添加结算信息"), HttpStatus.OK);
            } else {
                assetDetailService.createSettleAccount(createSettleAccountDto, asset);

                return new ResponseEntity<>(BaseResult.success("添加结算成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("结算信息更新")
    @PostMapping("/update-settle-account")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> updateSettleAccount(@Validated @RequestBody UpdateSettleAccountDto updateSettleAccountDto, @CurrentUser User user) {
        SettleAccount settleAccount = assetDetailService.getSettleAccountById(updateSettleAccountDto.getId());

        if (settleAccount == null) {
            return new ResponseEntity<>(BaseResult.error("结算信息不存在"), HttpStatus.OK);
        } else {
            Asset asset = assetDetailService.getAssetById(settleAccount.getAsset_id());

            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法更新结算信息"), HttpStatus.OK);
            } else {
                assetDetailService.updateSettleAccount(updateSettleAccountDto);

                return new ResponseEntity<>(BaseResult.success("结算信息更新成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("删除结算信息")
    @PostMapping("/delete-settle-account")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> deleteSettleAccount(@Validated @RequestBody IdDto idDto, @CurrentUser User user) {
        SettleAccount settleAccount = assetDetailService.getSettleAccountById(idDto.getId());

        if (settleAccount == null) {
            return new ResponseEntity<>(BaseResult.error("结算信息不存在"), HttpStatus.OK);
        } else {
            Asset asset = assetDetailService.getAssetById(settleAccount.getAsset_id());

            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法删除结算信息"), HttpStatus.OK);
            } else {
                assetDetailService.deleteSettleAccount(settleAccount.getId(), settleAccount.getAsset_id());

                return new ResponseEntity<>(BaseResult.success("结算信息删除成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("获取结算详情")
    @GetMapping("/settle-account-detail")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> settleAccountDetail(@Validated IdDto idDto, @CurrentUser User user) {
        SettleAccount settleAccount = assetDetailService.getSettleAccountById(idDto.getId());

        if (settleAccount == null) {
            return new ResponseEntity<>(BaseResult.error("结算信息不存在"), HttpStatus.OK);
        } else {
            SettleAccountDetail settleAccountDetail = assetDetailService.getSettleAccountDetail(settleAccount);

            return new ResponseEntity<>(BaseResult.success("获取结算详情成功").setData(settleAccountDetail), HttpStatus.OK);
        }
    }

    @ApiOperation("添加回款")
    @PostMapping("/add-fund-restream")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> addFundRestream(@Validated @RequestBody CreateFundRestreamDto createFundRestreamDto, @CurrentUser User user) {
        Asset asset = assetDetailService.getAssetById(createFundRestreamDto.getAsset_id());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法添加回款信息"), HttpStatus.OK);
            } else {
                Boolean success = assetDetailService.createFundRestream(asset, createFundRestreamDto, FundRestreamSourceType.DEBTOR);

                if (success) {
                    return new ResponseEntity<>(BaseResult.success("添加回款成功"), HttpStatus.OK);
                } else {
                    return new ResponseEntity<>(BaseResult.error("添加回款失败"), HttpStatus.OK);
                }
            }
        }
    }

    @ApiOperation("回款信息更新")
    @PostMapping("/update-fund-restream")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> updateFundRestream(@Validated @RequestBody UpdateFundRestreamDto updateFundRestreamDto, @CurrentUser User user) {
        FundRestream fundRestream = assetDetailService.getFundRestreamById(updateFundRestreamDto.getId());

        if (fundRestream == null) {
            return new ResponseEntity<>(BaseResult.error("回款信息不存在"), HttpStatus.OK);
        } else {
            Asset asset = assetDetailService.getAssetById(fundRestream.getAsset_id());

            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法更新回款信息"), HttpStatus.OK);
            } else {
                assetDetailService.updateFundRestream(updateFundRestreamDto, asset);

                return new ResponseEntity<>(BaseResult.success("回款信息更新成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("删除回款信息")
    @PostMapping("/delete-fund-restream")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> deleteFundRestream(@Validated @RequestBody IdDto idDto, @CurrentUser User user) {
        FundRestream fundRestream = assetDetailService.getFundRestreamById(idDto.getId());

        if (fundRestream == null) {
            return new ResponseEntity<>(BaseResult.error("回款信息不存在"), HttpStatus.OK);
        } else {
            Asset asset = assetDetailService.getAssetById(fundRestream.getAsset_id());

            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法删除回款信息"), HttpStatus.OK);
            } else {
                assetDetailService.deleteFundRestream(fundRestream.getId(), asset);

                return new ResponseEntity<>(BaseResult.success("回款信息删除成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("获取回款详情")
    @GetMapping("/fund-restream-detail")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> fundRestreamDetail(@Validated IdDto idDto, @CurrentUser User user) {
        FundRestream fundRestream = assetDetailService.getFundRestreamById(idDto.getId());

        if (fundRestream == null) {
            return new ResponseEntity<>(BaseResult.error("回款信息不存在"), HttpStatus.OK);
        } else {
            FundRestreamDetail fundRestreamDetail = assetDetailService.getFundRestreamDetail(fundRestream);

            return new ResponseEntity<>(BaseResult.success("获取回款详情成功").setData(fundRestreamDetail), HttpStatus.OK);
        }
    }

    @ApiOperation("添加备注")
    @PostMapping("/add-remark")
    public ResponseEntity<BaseResult> addRemark(@Validated @RequestBody CreateRemarkDto createRemarkDto, @CurrentUser User user) {
        Asset asset = assetDetailService.getAssetById(createRemarkDto.getAsset_id());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法添加备注信息"), HttpStatus.OK);
            } else {
                assetDetailService.createRemark(createRemarkDto, user);

                return new ResponseEntity<>(BaseResult.success("添加备注成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("备注信息更新")
    @PostMapping("/update-remark")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> updateRemark(@Validated @RequestBody UpdateRemarkDto updateRemarkDto, @CurrentUser User user) {
        Remark remark = assetDetailService.getRemarkById(updateRemarkDto.getId());

        if (remark == null) {
            return new ResponseEntity<>(BaseResult.error("备注信息不存在"), HttpStatus.OK);
        } else {
            Asset asset = assetDetailService.getAssetById(remark.getAsset_id());

            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法更新备注信息"), HttpStatus.OK);
            } else {
                assetDetailService.updateRemark(updateRemarkDto, user);

                return new ResponseEntity<>(BaseResult.success("备注信息更新成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("删除备注信息")
    @PostMapping("/delete-remark")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> deleteRemark(@Validated @RequestBody IdDto idDto, @CurrentUser User user) {
        Remark remark = assetDetailService.getRemarkById(idDto.getId());

        if (remark == null) {
            return new ResponseEntity<>(BaseResult.error("备注信息不存在"), HttpStatus.OK);
        } else {
            Asset asset = assetDetailService.getAssetById(remark.getAsset_id());

            if (asset.getStatus().equals(AssetStatusType.RECOVERED)) {
                return new ResponseEntity<>(BaseResult.error("资产已回收, 无法删除备注信息"), HttpStatus.OK);
            } else {
                assetDetailService.deleteRemark(remark.getId());

                return new ResponseEntity<>(BaseResult.success("备注信息删除成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("获取备注详情")
    @GetMapping("/remark-detail")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> remarkDetail(@Validated IdDto idDto, @CurrentUser User user) {
        Remark remark = assetDetailService.getRemarkById(idDto.getId());

        if (remark == null) {
            return new ResponseEntity<>(BaseResult.error("备注信息不存在"), HttpStatus.OK);
        } else {
            RemarkDetail remarkDetail = assetDetailService.getRemarkDetail(remark);

            return new ResponseEntity<>(BaseResult.success("获取备注详情成功").setData(remarkDetail), HttpStatus.OK);
        }
    }
}
